package com.lanou.tipsy.fragment.recommend;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.recommend.TodaySongsListsAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.entity.recommend.guess.songlist.GuessListEntity;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by FengJun on 15/10/24.
 * 推荐->猜你喜欢->今日歌单
 */
public class TodaySongsListFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "TodaySongsListFragment";
    private ArrayList<GuessListEntity> guessListEntities;
    private GuessListEntity entity;
    private TodaySongsListsAdapter adapter;
    private TextView songsCount;
    private RecyclerView recommendList;
    private SimpleDraweeView draweeView;
    private Toolbar toolbar;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_recommend_guess_songs_list, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        //获取上个页面传递过来的数据
        Bundle bundle = getArguments();
        guessListEntities = (ArrayList<GuessListEntity>) bundle.getSerializable("guessListEntities");
        Random random = new Random();
        int i = random.nextInt(guessListEntities.size() - 1);
        String url = guessListEntities.get(i).getPic_huge();
        if (url != null && url != "") {
            Uri uri = Uri.parse(url);
            draweeView.setImageURI(uri);
        }
        if (guessListEntities.size() > 0) {
            songsCount.setText("共" + guessListEntities.size() + "首歌");
        }
        //配置推荐列表
        adapter = new TodaySongsListsAdapter(context, guessListEntities);
        recommendList.setAdapter(adapter);
        recommendList.setLayoutManager(new LinearLayoutManager(context));
        recommendList.setItemAnimator(new DefaultItemAnimator());
        click();
    }

    private void initView(View view) {
        songsCount = (TextView) view.findViewById(R.id.tv_songs_count);
        recommendList = (RecyclerView) view.findViewById(R.id.rv_today_recommend);
        draweeView = (SimpleDraweeView) view.findViewById(R.id.iv_recommend_everyday);
        toolbar = (Toolbar) view.findViewById(R.id.tl_today);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);
    }

    private void click() {
        adapter.setOnItemClickLitener(new TodaySongsListsAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                entity = guessListEntities.get(position);
                Toast.makeText(context,
                        position + " click, 名字是" + entity.getTitle(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}
